<?php
class Advertisement extends DataObject{
	static $db = array (
		"Title"=>"Varchar(300)",
		"Link"=>"Varchar(300)",
		//"Left"=>"Boolean",
		//"Right"=>"Boolean",
		"Status"=>"Boolean",
	);
	static $has_one = array (
		'Image'=>'Image',
	);

	static $searchable_fields = array (
		'Title'=> array('title'=>'Tiêu đề(Tiếng việt)'),
	);
	/**
	 * Overload so that the default sort.
	 */
	static $default_sort = 'Created DESC';

	static $summary_fields = array (
		'Title' => 'Tiêu đề',
	);
	static $defaults = array(
		'Status' => 'True');
	/**
	 * Create field for CMS
	 */
	function getCMSFields($controller = null) {

		$ArticlePub= new Tab("Quảng cáo",
		new TextField("Title","Tiêu đề"),
		new TextField("Link","Link"),
	//	new CheckboxField("Left", _t('Article.STATUS', 'Hiển thị bên trái')),
	//	new CheckboxField("Right", _t('Article.STATUS', 'Hiển thị bên phải')),
		new CheckboxField("Status", _t('Article.STATUS', 'Hiển thị')),
		new ImageField("Image", _t('Article.IMAGE', 'Image'))
		);
		$fields = new FieldSet(new TabSet("Root",$ArticlePub));
		return $fields;
	}
	function getCMSValidator() {
		return new RequiredFields(array('Title'));
	}
	function canCreate() {return true;}
	function canEdit() {return true;}
	function canDelete() {return true;}
	public function getAdv()
	{
		$locale=$this->current_lang();
		$sqlQuery = new SQLQuery();
		$sqlQuery->select = array(
		"n.*",
		);
		$sqlQuery->from = array("Advertisement n");
		$sqlQuery->where = array("n.Status=1");
		$sqlQuery->limit = "2";
		$sqlQuery->orderby("n.Created DESC");
		// get the raw SQL
		$rawSQL = $sqlQuery->sql();
		$result = $sqlQuery->execute();
		$dataObject = new DataObjectSet();
		foreach($result as $row) {
			$row['Image']=ProcessFile::Link($row['ImageID']);
			$dataObject->push(new ArrayData($row));
		}
		if($dataObject)
		$dataObject->parseQueryLimit($sqlQuery);
		return $dataObject;
	}

}